package com.kexin.jbsg.guansdk;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.kexin.jbsg.guansdk.callback.LoginCallBack;
import com.kexin.jbsg.util.APNUtil;

public class MainActivity extends Activity implements OnClickListener{
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button click_login = (Button) findViewById(R.id.click_login);
		click_login.setOnClickListener(this);
		KomSDKInfo.initSDK(0);
		initNetwork();
	}


	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.click_login) {
		} else {
		}
		
	}
	
	/**
	 * 初始化网络
	 */
	public void initNetwork(){
		if (false == APNUtil.isNetworkAvailable(this)) {
            AlertDialog.Builder ab = new AlertDialog.Builder(this);
            ab.setMessage("网络未连接,请设置网络");
            ab.setPositiveButton("设置", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent("android.settings.SETTINGS");
                    startActivityForResult(intent, 0);
                }
            });
            ab.setNegativeButton("退出", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    System.exit(0);
                }
            });
            ab.show();
        } else {
        	initSDK(); //初始化sdk
        }
	}
	
	/**
	 * 初始化sdk
	 */
	public void initSDK(){
		KomSDKInfo.getInstance().openLoginActivity(this,new LoginCallBack() {
			
			@Override
			public void userLogin(String userAccount, int time, String sign) {
				Toast.makeText(MainActivity.this, "登录成功:"+userAccount+","+time+","+sign, Toast.LENGTH_LONG).show();
			}
			
			@Override
			public void userCancel() {
				Toast.makeText(MainActivity.this, "用户取消登录", Toast.LENGTH_LONG).show();
			}
		});
	}
	
	
	//打开其他界面返回数据
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode==KomSDKInfo.RESULT_CODE_LOGIN){
			Bundle bd = data.getExtras();
			if(bd!=null&&bd.containsKey("loginResult")){
				String userAccount = bd.getString("userAccount");
				String sign = bd.getString("sign");
				int time = bd.getInt("time");
				KomSDKInfo.instance.getLoginCallBack().userLogin(userAccount, time, sign);
			}else{
				KomSDKInfo.instance.getLoginCallBack().userCancel();
			}
		}
	}
	
	
	

}
